//
//  ShareAppViewController.swift
//  Victory
//
//  Created by CA on 16/1/4.
//  Copyright © 2016年 CA. All rights reserved.
//

import UIKit
import MBProgressHUD

enum ShareControllerType:Int {
    case sportReport = 0
    case sportSkill = 1
}

class ShareAppViewController: UIViewController, UIGestureRecognizerDelegate {
    
    @IBOutlet weak var springView: SpringView!
    
    @IBOutlet weak var springViewBottomCon: NSLayoutConstraint!
    
    @IBOutlet weak var shareView: UIView!
    
    @IBOutlet weak var wechatButton: UIButton!
    @IBOutlet weak var weChatLabel: UILabel!

    @IBOutlet weak var friendsButton: UIButton!
    @IBOutlet weak var friendsLabel: UILabel!
    @IBOutlet weak var QQButton: UIButton!
    @IBOutlet weak var QQLabel: UILabel!
    
    @IBOutlet weak var QQZoneButton: UIButton!
    @IBOutlet weak var QQZoneLabel: UILabel!
    @IBOutlet weak var cancelButton: DesignableButton!
    
    var detailReportModel: SportDetailReportModel?
    
    var showHud:(()->())? = nil
    
    public var dayTimeStamp:Int = 0
    public var shareControllerType:Int = ShareControllerType.sportSkill.rawValue
   
    
    // MARK: Constants
    fileprivate struct Constants {
        static let WeiXinShareButtonTag = 100
        static let QQZoneShareButtonTag = 101
        static let QQShareButtonTag = 102
        static let PenYouQuanButtonTag = 103
        static let FacebookButtonTag = 104
    }

    // MARK: View LifeCycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !Bundle.currentLanguageIsChinese() {
            self.wechatButton .setImage(UIImage(named: "facebook_btn_fb"), for: UIControlState.normal)
            self.weChatLabel.text = "Facebook";
            self.QQButton.isHidden = true
            self.QQLabel.isHidden = true
            self.friendsButton.isHidden = true
            self.friendsLabel.isHidden = true
            self.QQZoneButton.isHidden = true
            self.QQZoneLabel.isHidden = true
        }
        self.cancelButton .setTitle(NSLocalizedString("Cancel", comment: ""), for: UIControlState.normal)
        addTapGestureRecognzier()
    }
    
    // MARK: IBAction
    @IBAction func shareButtonDidTouch(_ sender: UIButton) {
        switch sender.tag {
        case Constants.WeiXinShareButtonTag, Constants.PenYouQuanButtonTag:
            if Bundle.currentLanguageIsChinese() {
                if !WXApi.isWXAppInstalled() {
                    MBProgressHUD.showTextHUD(withMessage: "用户没有安装\"微信\"应用", in: view)
                    return
                }
            }
        case Constants.QQShareButtonTag, Constants.QQZoneShareButtonTag:
            if !QQApiInterface.isQQInstalled() {
                MBProgressHUD.showTextHUD(withMessage: "用户没有安装\"QQ\"应用", in: view)
                return
            }
        default: break
        }
        
        self.dismiss(animated: false) {
            if self.showHud != nil {
                self.showHud!()
            }
        }
        
        self.dismiss(animated: false, completion: nil)
        switch sender.tag {
        case Constants.WeiXinShareButtonTag:
            if Bundle.currentLanguageIsChinese() {
                print("微信")
                self.performShareController(with: SSDKPlatformType.typeWechat)
            }else {
                print("FaceBook")
                self.performShareController(with: SSDKPlatformType.typeFacebook)
            }
        case Constants.QQZoneShareButtonTag:
            print("QQ空间")
            self.performShareController(with: SSDKPlatformType.subTypeQZone)
        case Constants.QQShareButtonTag:
            print("QQ分享")
             self.performShareController(with: SSDKPlatformType.subTypeQQFriend)
        case Constants.PenYouQuanButtonTag:
            print("朋友圈")
            self.performShareController(with: SSDKPlatformType.subTypeWechatTimeline)
        default: break
        }
    }
    
    func performShareController(with type:SSDKPlatformType) {
        
        let storyBoard = UIStoryboard.init(name: "ReportShare", bundle: nil)
        
        if self.shareControllerType == ShareControllerType.sportSkill.rawValue {
            let skillVc:SportSkillShareController = storyBoard.instantiateViewController(withIdentifier: "SportSkillShareController") as! SportSkillShareController
            skillVc.detailReportModel = self.detailReportModel
            skillVc.type = type
            skillVc.dismissBlock = {
                self.dismissController()
            }
            self.present(skillVc, animated: true, completion: nil)
        }else {
            let reportVc:SportReportShareController = storyBoard.instantiateViewController(withIdentifier: "SportReportShareController") as! SportReportShareController
            reportVc.detailReportModel = self.detailReportModel
            reportVc.selectedDayTimeStamp = Int64(self.dayTimeStamp)
            reportVc.type = type
            reportVc.dismissBlock = {
                self.dismissController()
            }
            self.present(reportVc, animated: true, completion: nil)
        }
        
    }
    
    
    
    @IBAction func cancelButtonDidTouch() {
        dismissController()
    }
    
    // MARK: Private Method
    fileprivate func addTapGestureRecognzier() {
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ShareAppViewController.dismissController))
        tapGestureRecognizer.cancelsTouchesInView = true
        tapGestureRecognizer.delegate = self
        view.addGestureRecognizer(tapGestureRecognizer)
    }
    
    
    @objc fileprivate func dismissController() {
        self.dismiss(animated: true, completion: nil)
    }
    
    // MARK: UIGestureRecognizerDelegate
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return touch.view != shareView
    }
    
}
